import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 菜单管理
 * guopeng
 * 2017.11.27
 */
export class MenuData{
  constructor(
    private menuId : string,//菜单id
    private parentMenuId : string,//父菜单id
    private menuName : string,//菜单名
    private menuUrl :string,//菜单地址
    private menuIcon :string,//菜单图标
    private menuStatus : string,//菜单状态，0：启用，1：停用
    private menuOrder : string,//菜单排序
    private createTime : string,//创建时间
    private updateTime : string,//更新时间
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  selector: 'app-menuData',
  templateUrl: './menuData.component.html',
  styleUrls: ['./menuData.component.css']
})
export class MenuDataComponent implements OnInit{
  private menuName : string;//菜单名
  private menuUrl :string;//菜单地址
  private menuStatus : string;//菜单状态，0：启用，1：停用
  errorMessage:string;//錯誤消息
  private list:Array<any>;
  public queryMessage:MenuData = new MenuData("","","","","","","","","","");
  public updateMessages:MenuData = new MenuData("","","","","","","","","","");
  public addMessages:MenuData =  new MenuData("","","","","","","","","","");
  private MenuDataList : MenuData;

  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new MenuData("","","","","","","","","","");
    this.http.get(this.url+'/menu/queryMenuListByPage').subscribe(data => {
      this.MenuDataList = data['records'];
    })
  }

  userMessage(event:any){
    let query = new MenuData("","","","","","","","","","");
    let update = new MenuData("","","","","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
    this.addMessages =  new MenuData("","","","","","","","","","");
    this.http.get(this.url+'/menu/queryMenuListByPage').subscribe(data => {
      this.MenuDataList = data['records'];
    })
  }

  listMessage(info:any){
    let menuName = info.menuName== undefined ? '' : info.menuName ;
    let menuUrl = info.menuUrl== undefined ? '' : info.menuUrl;
    let menuStatus = info.menuStatus== undefined ? '' : info.menuStatus;
    this.http.get(this.url+'/menu/queryMenuListByPage?menuName='+menuName+'&menuUrl='+menuUrl+'&menuStatus='+menuStatus).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/menu/addMenu',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101114001'){
          this.errorMessage = "菜单名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114002'){
          this.errorMessage = "菜单地址为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114003'){
          this.errorMessage = "菜单地址已存在";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114004'){
          this.errorMessage = "菜单状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114005'){
          this.errorMessage = "菜单序号为空";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/menu/deleteMenu/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $("#update").modal('show');
      }else if(data['resultCode']=='101114006'){
        this.errorMessage = "该菜单不存在";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/menu/updateMenu',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          alert("修改成功！");
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101114001'){
          this.errorMessage = "菜单名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114002'){
          this.errorMessage = "菜单地址为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114003'){
          this.errorMessage = "菜单地址已存在";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114004'){
          this.errorMessage = "菜单状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114005'){
          this.errorMessage = "菜单序号为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101114006'){
          this.errorMessage = "该菜单不存在";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let menuName = this.menuName== undefined ? '' : this.menuName ;
    let menuUrl = this.menuUrl== undefined ? '' : this.menuUrl;
    let menuStatus = this.menuStatus== undefined ? '' : this.menuStatus;
    let current = pageNo;
    this.http.get(this.url+'/menu/queryMenuListByPage?menuName='+menuName+'&menuUrl='+menuUrl+'&menuStatus='+menuStatus+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/menu/queryMenuListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}



